﻿using System;
using System.Data;

namespace SystemModule
{
    public static class DataReaderExtension
    {
        public static string GetString(this IDataReader dr, string name)
        {
            return Convert.ToString(dr[name]);
        }

        public static int GetInt32(this IDataReader dr, string name)
        {
            var ordinal = dr.GetOrdinal(name);
            return dr.IsDBNull(ordinal) ? 0 : dr.GetInt32(ordinal);
        }

        public static byte GetByte(this IDataReader dr,string name)
        {
            var ordinal = dr.GetOrdinal(name);
            return dr.IsDBNull(ordinal) ? (byte)0 : dr.GetByte(ordinal);
        }

        public static sbyte GetSByte(this IDataReader dr, string name)
        {
            var ordinal = dr.GetOrdinal(name);
            return dr.IsDBNull(ordinal) ? (sbyte)0 : (sbyte)dr.GetInt32(ordinal);
        }

        public static ushort GetUInt16(this IDataReader dr, string name)
        {
            var ordinal = dr.GetOrdinal(name);
            return dr.IsDBNull(ordinal) ? (ushort)0 : (ushort)dr.GetInt32(ordinal);
        }

        public static uint GetUInt32(this IDataReader dr,string name)
        {
            return Convert.ToUInt32(dr[name]);
        }
    }
}
